package coupon

import (
	"context"
	"mall/app/coupon/model"
	"time"

	"mall/app/coupon/cmd/api/internal/svc"
	"mall/app/coupon/cmd/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type AddSeckillSessionLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewAddSeckillSessionLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddSeckillSessionLogic {
	return &AddSeckillSessionLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *AddSeckillSessionLogic) AddSeckillSession(req *types.AddSeckillSessionReq) error {
	startTime, err := time.ParseInLocation("2006-01-02 03:04", req.StartTime, time.Local)
	endTime, err := time.ParseInLocation("2006-01-02 03:04", req.EndTime, time.Local)
	if err != nil {
		l.Error(err)
	}
	_, err = l.svcCtx.SeckillSessionModel.Insert(l.ctx, &model.SmsSeckillSession{
		Name:       req.Name,
		StartTime:  startTime,
		EndTime:    endTime,
		Status:     req.Status,
		CreateTime: time.Now(),
	})
	if err != nil {
		l.Error(err)
	}
	return nil
}
